#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Viktor Szakats, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################

# 定义一个 CMake 选项，用于控制是否安装 fish shell 的命令补全脚本，默认关闭
option(CURL_COMPLETION_FISH "Install fish completions" OFF)
# 定义一个 CMake 选项，用于控制是否安装 zsh shell 的命令补全脚本，默认关闭
option(CURL_COMPLETION_ZSH "Install zsh completions" OFF)

# 若用户选择安装 fish 或 zsh 的命令补全脚本
if(CURL_COMPLETION_FISH OR
   CURL_COMPLETION_ZSH)
  # 若系统中找到了 Perl 解释器
  if(PERL_FOUND)
    # 若用户选择安装 fish 的命令补全脚本
    if(CURL_COMPLETION_FISH)
      # 设置生成的 fish 补全脚本文件名
      set(_completion_fish "curl.fish")
      # 添加自定义命令，用于生成 fish 补全脚本
      add_custom_command(OUTPUT "${_completion_fish}"
        # 执行 completion.pl 脚本，指定命令行选项文档目录和 shell 类型为 fish，将输出重定向到 curl.fish 文件
        COMMAND "${PERL_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/completion.pl"
          --opts-dir "${PROJECT_SOURCE_DIR}/docs/cmdline-opts" --shell fish > "${_completion_fish}"
        # 该命令依赖于 completion.pl 脚本
        DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/completion.pl"
        # 确保命令参数按原样传递
        VERBATIM
      )
      # 添加自定义目标，确保每次构建时都会生成 fish 补全脚本
      add_custom_target("curl-completion-fish" ALL DEPENDS "${_completion_fish}")
    endif()

    # 若用户选择安装 zsh 的命令补全脚本
    if(CURL_COMPLETION_ZSH)
      # 设置生成的 zsh 补全脚本文件名
      set(_completion_zsh "_curl")
      # 添加自定义命令，用于生成 zsh 补全脚本
      add_custom_command(OUTPUT "${_completion_zsh}"
        # 执行 completion.pl 脚本，指定命令行选项文档目录和 shell 类型为 zsh，将输出重定向到 _curl 文件
        COMMAND "${PERL_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/completion.pl"
          --opts-dir "${PROJECT_SOURCE_DIR}/docs/cmdline-opts" --shell zsh > "${_completion_zsh}"
        # 该命令依赖于 completion.pl 脚本
        DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/completion.pl"
        # 确保命令参数按原样传递
        VERBATIM
      )
      # 添加自定义目标，确保每次构建时都会生成 zsh 补全脚本
      add_custom_target("curl-completion-zsh" ALL DEPENDS "${_completion_zsh}")
    endif()

    # 若未禁用安装操作
    if(NOT CURL_DISABLE_INSTALL)
      # 若不是交叉编译环境
      if(NOT CMAKE_CROSSCOMPILING)
        # 若未指定 fish 补全脚本的安装目录
        if(NOT CURL_COMPLETION_FISH_DIR)
          # 安静模式查找 PkgConfig 模块
          find_package(PkgConfig QUIET)
          # 使用 pkg-config 获取 fish shell 补全脚本的安装目录
          pkg_get_variable(CURL_COMPLETION_FISH_DIR "fish" "completionsdir")
          # 若未成功获取到 fish 补全脚本的安装目录
          if(NOT _pkg_fish_completionsdir)
            # 包含 GNU 安装目录模块
            include(GNUInstallDirs)
            # 若指定了数据根目录
            if(CMAKE_INSTALL_DATAROOTDIR)
              # 设置 fish 补全脚本的安装目录
              set(CURL_COMPLETION_FISH_DIR "${CMAKE_INSTALL_DATAROOTDIR}/fish/vendor_completions.d")
            endif()
          endif()
        endif()
        # 若未指定 zsh 补全脚本的安装目录
        if(NOT CURL_COMPLETION_ZSH_DIR)
          # 包含 GNU 安装目录模块
          include(GNUInstallDirs)
          # 若指定了数据根目录
          if(CMAKE_INSTALL_DATAROOTDIR)
            # 设置 zsh 补全脚本的安装目录
            set(CURL_COMPLETION_ZSH_DIR "${CMAKE_INSTALL_DATAROOTDIR}/zsh/site-functions")
          endif()
        endif()
        # 若已确定 fish 补全脚本的安装目录
        if(CURL_COMPLETION_FISH_DIR)
          # 安装生成的 fish 补全脚本到指定目录
          install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${_completion_fish}"
                  DESTINATION "${CURL_COMPLETION_FISH_DIR}")
        endif()
        # 若已确定 zsh 补全脚本的安装目录
        if(CURL_COMPLETION_ZSH_DIR)
          # 安装生成的 zsh 补全脚本到指定目录
          install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${_completion_zsh}"
                  DESTINATION "${CURL_COMPLETION_ZSH_DIR}")
        endif()
      else()
        # 若处于交叉编译环境，输出状态信息提示无法安装补全脚本
        message(STATUS "We cannot install completion scripts when cross-compiling")
      endif()
    endif()
  else()
    # 若未找到 Perl 解释器，输出警告信息提示无法生成补全脚本
    message(WARNING "No perl: cannot generate completion script")
  endif()
endif()
